/*
 * 编辑角色
 */
define(function(require, exports, module) {
    require("../../zTree_v3/js/jquery.ztree.all-3.5.min.js");
    require("common/umei.dev");
    // var umei_dialog = require("common/umei.dialog");
    // var ajax = require("common/umei.ajax");
    // var umei_form = require("common/umei.form");

    var roleId = $("#roleId").val();

    var url = "role_add.do";
    if (roleId != "") {
	url = "role_update.do";
    }
    var setting = {
	view : {
	    selectedMulti : false
	}, check : {
	    enable : true, chkStyle : "checkbox", chkboxType : {
		"Y" : "ps", "N" : "ps"
	    }
	}, data : {
	    simpleData : {
		enable : true
	    }
	}
    };

    loadZtree();
    umei.initForm({
	formObj : $("#role_form")
    });
    $("#subBtn").click(function() {
	if (umei.verification({
	    formObj : $("#role_form")
	})) {
	    var idsArray = [];
	    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	    var nodes = treeObj.getCheckedNodes(true);;
	    for (var i = 0, _len = nodes.length; i < _len; i++) {
		var node = nodes[i];
		idsArray.push(node.id);
	    }
	    if (idsArray.length == 0) {
		umei.tipMsg({
		    type : "warn", content : "请选择菜单", timeout : 3000
		});
		return;
	    }
	    var paramData = {
		"id" : $.trim($("#roleId").val()), "roleName" : $.trim($("#role_form input[name='roleName']").val()), "menuIds" : idsArray.join(",")
	    }
	    umei.postRequest({
		async : true, url : url, data : paramData, fun : function(response) {
		    umei.tipMsg({
			type : "success", content : "保存成功", timeout : 1500, fun : function() {
			    document.location.href = "role_list.do";
			}
		    });
		}
	    });
	}
    });

    function loadZtree() {
	var paramData = {
	    "roleId" : roleId
	};
	umei.postRequest({
	    async : true, url : umei.url.loadMenuUrl, data : paramData, fun : function(response) {
		var zNodes = response.data;
		var rootNode = {
		    id : 0, pId : -1, name : "所有菜单", open : true
		};
		zNodes.push(rootNode);
		var treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
		treeObj.expandAll(true);
	    }
	});
    }
});
